package client.gui;

/*Copyright 2010 Dietmar Wieser, Margit Mutschlechner and Florian Häser. All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this list of
 conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice, this list
 of conditions and the following disclaimer in the documentation and/or other materials
 provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY Dietmar Wieser, Margit Mutschlechner and Florian Häser ``AS IS''
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 Dietmar Wieser, Margit Mutschlechner and Florian Häser OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 The views and conclusions contained in the software and documentation are those of the
 authors and should not be interpreted as representing official policies, either expressed
 or implied, of Dietmar Wieser, Margit Mutschlechner and Florian Häser.
 */
import java.io.IOException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import client.Controller;
import client.DBLPClient;
import client.logic.clientmodel.ClientAuthorResponse;
import client.logic.clientmodel.ClientLoginResponse;
import client.logic.clientmodel.SuggestedAuthor;

/**
 * The GUIController class handles the access to the graphical user interface
 * 
 * @author Dietmar Wieser
 */
public class GUIController implements GUIControllerInterface {

	/** the instance of the GUIController */
	private static GUIController INSTANCE = null;
	/** the progress dialog that is shown when waiting for the answer of the server */
	private static ProgressDialog pd;

	/** initializes the instance of the GUIController */
	public static void initGUIController() {
		if (INSTANCE == null) {
			INSTANCE = new GUIController();
		}
	}

	public static GUIController getInstance() {
		return INSTANCE;
	}

	/** Method to exit the application. */
	public void exit() {
//		INSTANCE=null;
		Log.i("","GUIController.exit");
		Controller.getInstance().exit();
	}

	/**
	 * Displays the List of the requested authors.
	 * 
	 * @param car
	 *            the list of all requested authors with their attributes.
	 * @param isBlueToothRequest
	 *            true if the bluetooth range was scanned, false otherwise.
	 * */
	public void showAuthorList(ClientAuthorResponse car,
			boolean isBlueToothRequest) {
		Bundle bundle = new Bundle();
		if (car != null)
			bundle.putByteArray("car", car.persist());
		bundle.putBoolean("isBlueToothRequest", isBlueToothRequest);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.AuthorList.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Displays a co-publications, coco-publications or topic-list.
	 * 
	 * @param title
	 *            the title of the screen.
	 * @param type
	 *            1=co-publications, 2=coco-publications, topic otherwise.
	 * @param sa
	 *            the author-object
	 * @param car
	 *            the whole author response
	 * @param isBlueToothRequest
	 *            true if the bluetooth range was scanned, false otherwise.
	 * */
	public void showAuthorContentDetailForm(String title, byte type,
			SuggestedAuthor sa, ClientAuthorResponse car,
			boolean isBlueToothRequest) {
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putByte("type", type);
		try {
			if (sa != null)
				bundle.putByteArray("sa", sa.persist());
		} catch (IOException e) {
			System.out.println("Error while persisting suggested author");
		}
		if (car != null)
			bundle.putByteArray("car", car.persist());
		bundle.putBoolean("isBlueToothRequest", isBlueToothRequest);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.AuthorContentDetailForm.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Displays the content of a selected author.
	 * 
	 * @param sa
	 *            the author-object
	 * @param car
	 *            the whole author response
	 * @param isBlueToothRequest
	 *            true if the bluetooth range was scanned, false otherwise.
	 */
	public void showAuthorContentForm(SuggestedAuthor sa,
			ClientAuthorResponse car, boolean isBlueToothRequest) {
		Bundle bundle = new Bundle();
		try {
			if (sa != null)
				bundle.putByteArray("sa", sa.persist());
		} catch (IOException e) {
			System.out.println("Error while persisting suggested author");
		}
		if (car != null)
			bundle.putByteArray("car", car.persist());
		bundle.putBoolean("isBlueToothRequest", isBlueToothRequest);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.AuthorContentForm.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Displays the login screen.
	 * 
	 * @param conferences
	 *            a list of all active conferences.
	 * @param firstName
	 *            the firstname of the user.
	 * @param lastName
	 *            the lastname of the user.
	 * */
	public void showLoginForm(String[] conferences, String firstName,
			String lastName) {
		Bundle bundle = new Bundle();
		bundle.putStringArray("conferences", conferences);
		bundle.putString("firstName", firstName);
		bundle.putString("lastName", lastName);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.LoginForm.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Displays the second login screen.
	 * 
	 * @param conference
	 *            the selected conference.
	 * @param clr
	 *            the login response from the server.
	 * */
	public void showLoginForm2(String conference, ClientLoginResponse clr) {
		Bundle bundle = new Bundle();
		bundle.putString("conference", conference);
		if (clr != null)
			bundle.putByteArray("clr", clr.persist());
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.LoginForm2.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/** Displays the main screen of the application. */
	public void showMainForm() {
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.MainForm.class);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Displays a progress dialog on the screen.
	 * @param c the context in which the dialog is shown.
	 * @param title
	 *            title of the alert.
	 * @param text
	 *            the content of the alert.
	 * */
	public void showProgressDialog(Context c, String title, String text) {
		pd = ProgressDialog.show(c, title, text, true, false);
	}

	/**
	 * Dismisses the progress dialog.
	 */
	public void dismissProgressDialog() {
		pd.dismiss();
	}

	/**
	 * Initializes the actual user profile
	 * 
	 * @param firstName
	 *            the firstname of the user.
	 * @param lastName
	 *            the lastname of the user.
	 * @param conf
	 *            the selected conference.
	 * @param authorId
	 *            the database-id of the current user.
	 * */
	public void setActualClientProfile(String firstName, String lastName,
			String conf, long authorId) {
		Controller.getInstance().setActualClientProfile(firstName, lastName,
				conf, authorId);
	}

	/** Requests the adequate authors from the server. */
	public void requestAuthors() {
		DBLPClient.setRestart1(false);
		DBLPClient.setRestart2(true);
		ClientAuthorResponse car = Controller.getInstance().requestAuthors();
		Bundle bundle = new Bundle();
		if (car != null)
			bundle.putByteArray("car", car.persist());
		bundle.putBoolean("isBlueToothRequest", false);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.AuthorList.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Requests all publications of a selected author.
	 * 
	 * @param sa
	 *            the selected author.
	 * @param car
	 *            the whole author response.
	 * @param isBlueToothRequest
	 *            true if the bluetooth range was scanned, false otherwise.
	 * */
	public void requestPublications(SuggestedAuthor sa,
			ClientAuthorResponse car, boolean isBlueToothRequest) {
		String[] publications = Controller.getInstance().requestPublications(
				sa.getAuthor());
		Bundle bundle = new Bundle();
		try {
			if (sa != null)
				bundle.putByteArray("sa", sa.persist());
		} catch (IOException e) {
			System.out.println("Error while persisting suggested authors");
		}
		if (car != null)
			bundle.putByteArray("car", car.persist());
		bundle.putBoolean("isBlueToothRequest", isBlueToothRequest);
		bundle.putStringArray("publications", publications);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.PublicationsForm.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/**
	 * Stores the name of the current user.
	 * 
	 * @param firstName
	 *            the firstname of the user.
	 * @param lastName
	 *            the lastname of the user.
	 * */
	public void storeAuthorName(String firstName, String lastName) {
		Controller.getInstance().storeAuthorName(firstName, lastName);
	}

	/**
	 * Displays a toast on the screen.
	 * @param text
	 *            the content of the toast.
	 * */
	public void showToast(String text) {
		Toast.makeText(Controller.getInstance().getContext(), text,
				Toast.LENGTH_SHORT).show();
	}

	/** Displays the whole list of requested authors. */
	public void showCompleteAuthorList() {
		ClientAuthorResponse car = Controller.getInstance()
				.getClientAuthorResponse();
		Bundle bundle = new Bundle();
		if (car != null)
			bundle.putByteArray("car", car.persist());
		bundle.putBoolean("isBlueToothRequest", false);
		Intent i = new Intent(Controller.getInstance().getContext(),
				client.gui.forms.AuthorList.class);
		i.putExtras(bundle);
		Controller.getInstance().getContext().startActivity(i);
	}

	/** Scans the bluetooth range. */
	public void discoverDevices() {
		Controller.getInstance().discoverDevices();
	}

	/**
	 * Requests the login.
	 * 
	 * @param firstName
	 *            the firstname of the user.
	 * @param lastName
	 *            the lastname of the user.
	 * @param conference
	 *            the selected conference.
	 * */
	public void requestLogin(String firstName, String lastName,
			String conference) {
		Controller.getInstance().requestLogin(firstName, lastName, conference);
	}
}
